package com.myapp.mobilesafe.activity;

import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.myapp.mobilesafe.db.dao.AdressDao;

public class AddressQueryActivity extends AppCompatActivity {
    private EditText etNumber;
    private Button btnStart;
    private TextView tvResult;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_query);
        etNumber = (EditText) findViewById(R.id.et_number);
        btnStart = (Button) findViewById(R.id.btn_start);
        tvResult = (TextView) findViewById(R.id.tv_result);

        btnStart.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String number = etNumber.getText().toString().trim();
                if (!TextUtils.isEmpty(number)) {
                    String address = AdressDao.getAddress(number);
                    tvResult.setText(address);
                } else {
                    Animation shake = AnimationUtils.loadAnimation(
                            getApplicationContext(), R.anim.shake);
                    Toast.makeText(getApplicationContext(),"内容不能为空！！",Toast.LENGTH_SHORT).show();
                    etNumber.startAnimation(shake);
                    vibrator();
                }
            }
        });
        etNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String address = AdressDao.getAddress(s.toString());
                tvResult.setText(address);
            }
        });
    }
    private void vibrator() {
        // 振动器
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        // vibrator.vibrate(2000);// 振动一定时间
        vibrator.vibrate(new long[] { 1000, 2000, 2000, 3000 }, 2);// 参1:振动模式,奇数位表示休息时间,偶数为表示振动时间
        // 参2:-1表示不重复,
        // 0表示从第0个位置开始重复
        // vibrator.cancel();//停止振动
    }
}
